package com.itheima.controller;

import com.itheima.pojo.Question;
import com.itheima.pojo.Result;
import com.itheima.service.QuestionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("search")
@Slf4j
public class SearchController {

    @Autowired
    private QuestionService questionService;

    @PostMapping("/addQuestion")
    public Result AddQuestion(@RequestBody Question question){
        log.info("搜索服务端接收到要添加的问题是: {}",question);
        questionService.addQuestion(question);
        return new Result(true,"添加成功");
    }

    @GetMapping("/findAnswerByTitle/{title}")
    public Result findByTitle(@PathVariable String title){
        log.info("接收到要查询的条件为{}",title);
        if (null ==title){
            return new Result(false,"查询问题不能为空");
        }
        List<String> answers= questionService.findAnswerByTitle(title);
        if (answers.size()== 0 && answers.isEmpty()){
            return new Result(false,"没有答案");
        }
        return new Result(true,"查询成功",answers);
    }
}
